package cn.e4j.modular.crm.controller.base;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.common.exception.BizExceptionEnum;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.LangUtils;
import cn.e4j.model.exception.ServiceException;
import cn.e4j.modular.crm.entity.GwelT;
import cn.e4j.modular.crm.service.impl.Gwei015ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

/**
 * 日汇率控制类
 *
 * @author yuan_rz
 * @Date 2019/01/10 16:22
 */
@Controller
@RequestMapping("/base/gwei015")
public class Gwei015Controller extends BaseController {

    @Autowired
    private Gwei015ServiceImpl gwei015Service;

    /**
     * 跳转页面
     */
    @RequiresPermissions("base:gwei015:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/base/gwei015.html";
    }

    /**
     * 查询列表数据
     */
    @RequestMapping("/getListData")
    @RequiresPermissions("base:gwei015:init")
    @ResponseBody
    public ResponseData getListData(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = gwei015Service.getListData(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 导出Excel
     */
    @RequestMapping(value="/exportListData",method=RequestMethod.GET)
    @RequiresPermissions("base:gwei015:init")
    public void exportListData(ModelMap map, HttpServletRequest request, HttpServletResponse response){

    }

    /**
     * 添加数据
     */
    @RequestMapping("/addData")
    @RequiresPermissions("base:gwei015:init")
    @ResponseBody
    public ResponseData addData(GwelT gwelT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        gwelT.setGWELENT(shiroUser.getDBC());
        if(gwei015Service.existedTheParams(gwelT)){
            return new ErrorResponseData(BizExceptionEnum.EXISTED_THE_PARAMS.getCode() ,
                    BizExceptionEnum.EXISTED_THE_PARAMS.getMessage());
        }
        gwei015Service.addData(gwelT);
        return SUCCESS_TIP;
    }

    /**
     * 修改数据
     */
    @RequestMapping("/editData")
    @RequiresPermissions("base:gwei015:init")
    @ResponseBody
    public ResponseData editData(GwelT gwelT,@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        GwelT oldGwelT = new GwelT();
        oldGwelT.setGWELENT(gwelT.getGWELENT());
        oldGwelT.setGWEL001(inDto.getString("oldGWEL001"));
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        oldGwelT.setGWEL002(LocalDate.parse(inDto.getString("oldGWEL002"), formatter));
        if(!(gwelT.getGWEL001().equals(oldGwelT.getGWEL001())&&
                gwelT.getGWEL002().equals(oldGwelT.getGWEL002()))){
            if(gwei015Service.existedTheParams(gwelT)){
                return new ErrorResponseData(BizExceptionEnum.EXISTED_THE_PARAMS.getCode() ,
                        BizExceptionEnum.EXISTED_THE_PARAMS.getMessage());
            }
        }
        gwei015Service.editData(gwelT,oldGwelT);
        return SUCCESS_TIP;
    }

    /**
     * 删除数据
     */
    @RequestMapping("/deleteData")
    @RequiresPermissions("base:gwei015:init")
    @ResponseBody
    public ResponseData deleteData(GwelT gwelT) {
        gwei015Service.deleteData(gwelT);
        return SUCCESS_TIP;
    }

    /**
     * 批量删除数据
     */
    @RequestMapping("/deleteDataBatch")
    @RequiresPermissions("base:gwei015:init")
    @ResponseBody
    public ResponseData deleteDataBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwelT> gwelTList = inDto.getReqList(GwelT.class);
        gwei015Service.deleteDataBatch(gwelTList);
        return SUCCESS_TIP;
    }

}
